<?php
namespace Shopmanage\Controller;
use Think\Controller;
class JifenController extends ExtendController {
	public function index(){
		$TOOL = D("CheckTools");
		$TOOL->sid=$this->sid;
		$TOOL->ToolName="jifen";
		$TOOL->CheckToolStatus();
		if(!$TOOL->status){
			$this->success("您尚未开通积分插件，请开通插件再继续！","/?&c=chajian&m=shopmanage");
			die();
		}
		$TS = M("ToolsStatus");
		$cfg = json_decode($TS->where("`tool_id`=13 AND `sid`=".$this->sid)->getField("cfg"),true);
		if(!$cfg["dh"]){
			$cfg["dh"][0]["jifen"]=0;
			$cfg["dh"][0]["gift"]=0;
		}
		if(!$cfg["qddh"]){
			$cfg["qddh"][0]["tian"]=0;
			$cfg["qddh"][0]["gift"]=0;
		}
		if(!$cfg["jc"]){
			$cfg["jc"][0]["jine"]=100;
			$cfg["jc"][0]["jifen"]=1;
		}
		if(!$cfg["dddhjf"]){
			$cfg["dddhjf"][0]["ddje"]=1;
			$cfg["dddhjf"][0]["dhjf"]=1;
		}
		// 积分兑换读取整个系统中的奖品
		// $GIFTS = D("EventsGifts");
		// $GIFTS->wherestr = "`sid`=".$this->sid." AND `status`<>9";
		// $GIFTS->rollPage = $this->rollPage;
		// $GIFTS->listRows = $this->listRows;
		// $GIFTS->getgifts();
		$GIFT = M("Gifts");
		$gifts = $GIFT->where(Array("sid"=>$this->sid,"type"=>7))->getField("`id`,`level`,`name`,`type`");
		$info["msg"]="注意，积分签到、兑换不受活动时间影响！";
		$info["type"]="warning";
		$this->assign("info",$info);
		$this->assign("gifts",$gifts);
		$this->assign("gifts_json",htmlspecialchars(json_encode($gifts)));
		$this->assign("cfg",$cfg);
		$this->assign("title","积分配置");
		$this->assign("current","jifen");
		$this->display();
	}
	public function jifen_save(){
		$onoff = I("onoff");
		$dayset = I("day");
		$buqian = I("buqian");
		$buqianjifen = I("buqianjifen");
		$buqianjiac = I("buqianjiac");
		$jifen = I("duihuan");
		$gift = I("gift");
		$jcje = I("jcje");
		$jcjf = I("jcjf");
		$tian = I("tian");
		$qddhgift = I("qddhgift");
		$ddje = I("ddje");
		$dhjf = I("dhjf");
		$onoff_qdjc = I("onoff_qdjc");
		$onoff_dddhjf = I("onoff_dddhjf");
		$onoff_jfdh = I("onoff_jfdh");
		$onoff_qddh = I("onoff_qddh");
		$gifts = json_decode(htmlspecialchars_decode(I("gifts")),true);
		foreach($jifen as $k=>$fen){
			$dh[$k]["jifen"]=$fen;
			$dh[$k]["gift"]=$gift[$k];
			$dh[$k]["gift_name"]=$gifts[$gift[$k]]["name"];
			$dh[$k]["gift_type"]=$gifts[$gift[$k]]["type"];
		}
		foreach($jcje as $k=>$jc){
			$jcset[$k]["jine"]=$jc;
			$jcset[$k]["jifen"]=$jcjf[$k];
		}
		foreach($tian as $k=>$tia){
			$qddhset[$k]["tian"]=$tia;
			$qddhset[$k]["gift"]=$qddhgift[$k];
		}
		foreach($ddje as $k=>$ddj){
			$dddhjf[$k]["ddje"]=$ddj;
			$dddhjf[$k]["dhjf"]=$dhjf[$k];
		}
		$TS = M("ToolsStatus");
		$data["onoff"] = $onoff;
		$data["day"] = $dayset;
		$data["buqian"] = $buqian;
		$data["buqianjifen"] = $buqianjifen;
		$data["buqianjiac"] = $buqianjiac;
		$data["onoff_jfdh"]=$onoff_jfdh;
		$data["dh"]=$dh;
		$data["onoff_qdjc"]=$onoff_qdjc;
		$data["jc"]=$jcset;
		$data["onoff_qddh"]=$onoff_qddh;
		$data["qddh"]=$qddhset;
		$data["onoff_dddhjf"]=$onoff_dddhjf;
		$data["dddhjf"]=$dddhjf;
		$save["cfg"]=(json_encode($data));
		$TS->where("`tool_id`=13 AND `sid`=".$this->sid)->save($save);
		$this->success("该功能正在开发中，敬请期待哦","/?c=jifen&m=shopmanage");
	}
	public function user(){
		$TOOL = D("CheckTools");
		$TOOL->sid=$this->sid;
		$TOOL->ToolName="jifen";
		$TOOL->CheckToolStatus();
		if(!$TOOL->status){
			$this->success("您尚未开通积分插件，请开通插件再继续！","/?&c=chajian&m=shopmanage");
			die();
		}
		
		$usernick = I("usernick");
		Vendor('Page');
		$type=(int)I("get.type");
		$ctp=I("get.ctp");
		$ctp=$ctp?$ctp:"all";
		$tools = C("GFT_TYPE");
		if($ctp=="all"||!$ctp){
			$where="`sm_events_gifts`.`sid`=".$this->sid;
		}else{
			$where="`sm_events_gifts`.`sid`=".$this->sid." AND `sm_events_gifts`.`type`=".$tools[$ctp]["id"];
		}
		if($type){
			$where .= " AND `type`=9";
		}
		if($usernick){
			$where .=" AND `sm_user`.`usernick`='".urldecode_utf8($usernick)."'";
		}
		$E = D("UsersGifts");//users_gifts
		$E->sid=$this->sid;
		$E->where=$where." AND (`sm_users_gifts`.`getway`='jifen' OR `sm_users_gifts`.`getway`='qddh')";
		$E->usernick=$usernick;
		$E->GetTotal(); 
		$Page = new \Page($E->total,$this->listRows,Array("usernick"=>$usernick));
		$Page->rollPage=$this->rollPage;
		$show = $Page->show();
		$E->limit = $Page->firstRow.','.$Page->listRows;
		$data = $E->SendList();
		foreach($data as $k=>$gift){
			$data[$k]["actiontime"]=GetTimeInfo(strtotime($data[$k]["actiontime"]));
			$data[$k]["level"]=getlevel($data[$k]["level"]);
			foreach($tools as $tool){
				if($data[$k]["type"]==$tool["id"]){
					$data[$k]["type"]=$tool["name"];
				}
				// $data[$k]["getway"] = C("GIFT_GETWAY")[$data[$k]["getway"]];
				if($data[$k]["getway"]=="jifen"){
					$data[$k]["getway"]="积分兑换";
				}
				if($data[$k]["getway"]=="chouj"||empty($data[$k]["getway"])){
					$data[$k]["getway"]="自动抽奖";
				}
				if($data[$k]["getway"]=="shoud"){
					$data[$k]["getway"]="后台抽奖";
				}
				if($data[$k]["getway"]=="qddh"){
					$data[$k]["getway"]="签到兑换";
				}
			}
		}
		$this->assign('totalpg',(int)$Page->totalPages);
		$this->assign('curpg',$Page->nowPage);
		$this->assign('page',$show);
		$this->assign('ctp',$ctp);
		$this->assign('tools',$tools);
		$this->assign('data',$data);
		$this->assign('type',$type);
		$this->assign('search',$usernick);
		$this->assign('admin',"jifen");
		$this->assign("title","积分兑换");
		$this->assign("current","jifenuser");
		$this->display();
	}
	public function qiandao(){
		$usernick = I("usernick");
		$date = I("date");
		if($usernick){
		$Seeds = D("Shopmodule/User");
		$Seeds->usernick = urldecode_utf8($usernick);
		$Seeds->sid = $this->sid;
		$Seeds->curdate = $date;
		$Seeds->rili();
		$signlist = $Seeds->signlist;
		}
		
		$CAL = D("Calendar");
		$CAL->width="448px";
		$CAL->height="300px";
		$CAL->curdate=$date?$date:date("Y-m-d");
		$CAL->signday=$signlist;
		
		
		$this->assign("cal",$CAL->showCalendar());
		$this->assign("pre_mon",date("Y-m-d",strtotime($date." -1 month")));
		$this->assign("nex_mon",date("Y-m-d",strtotime($date." +1 month")));
		$this->assign("usernick",$usernick);
		$this->assign("title","签到检查");
		$this->assign("current","qiandao");
		$this->display();
	}
	public function jforders(){
		$usernick = I("post.usernick");
		$where = $usernick?" AND `buyer_nick`='$usernick'":"";
		Vendor('Page');
		$ORD = M("SeedsOrders");
		$total = $ORD->where("`sid`=".$this->sid.$where)->count();
		$Page = new \Page($total,$this->listRows);
		$Page->rollPage=$this->rollPage;
		$show = $Page->show();
		
		$orders = $ORD->where("`sid`=".$this->sid)->getField(" `id`,`buyer_nick`, `payment`, `oid`, `tid`, `type`, DATE_FORMAT(`pub_time`,'%Y-%m-%d') AS pub_time, `seeds`");
				
		$info["type"]="success";
		$info["msg"]="订单系统升级，仅抽过奖的订单号出现在这里。支持3个月以内的订单抽奖。";
		$this->assign('info',$info);
		$this->assign('totalpg',(int)$Page->totalPages);
		$this->assign('curpg',$Page->nowPage);
		$this->assign('page',$show);
		$this->assign("data",$orders);

		$this->assign('admin',"jifen");
		$this->assign("title","订单兑换积分记录");
		$this->assign("current","jforders");
		$this->display();
	}
	public function order_del(){
		$id = I("id");
		$ORD = M("SeedsOrders");
		$re = $ORD->where("`id`=".$id)->delete();
		if($re){
			$data["status"]=true;
			$data["msg"]="删除成功1order_del";
		}else{
			$data["status"]=false;
			$data["msg"]="删除失败";
		}
		$this->ajaxReturn($data);
	}
}